package top.hidoctor.auscultation.feature.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.util.AppUtil;
import com.tang.util.DialogUtil;
import com.tang.util.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.ShenheRwbAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.AuditListBean;
import top.hidoctor.auscultation.event.AddAuditSDEvent;
import top.hidoctor.auscultation.event.AuditZdEvent;
import top.hidoctor.auscultation.net.c.AuditListContract;
import top.hidoctor.auscultation.net.p.AuditListPresenter;
import top.hidoctor.auscultation.net.req.AuditReq;
import top.hidoctor.auscultation.view.ChooseShenhePop;

public class ShenhebaoActivity extends BaseActivity implements AuditListContract.View, View.OnClickListener {
    private TextView tvAdd, tvShaixuan;
    private RecyclerView recycler;
    private ShenheRwbAdapter adapter;
    private ChooseShenhePop chooseShenhePop;
    private SwipeRefreshLayout refreshLayout;
    private EditText editSearch;
    private AuditListPresenter presenter;
    private AuditReq req;
    private RadioButton rb_sh1, rb_sh2, rb_s1, rb_s2;
    private Button btnChongzhi, btnConfirm;
    private DrawerLayout drawer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_shenhe_bao);
        EventBus.getDefault().register(this);
        presenter = new AuditListPresenter(this, this);
        req = new AuditReq();
        initView();
    }

    private void initView() {
        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                hideSoft(toolbar);
            }
        });
        tvAdd = findViewById(R.id.tv_add);
        refreshLayout = findViewById(R.id.refreshLayout);
        tvShaixuan = findViewById(R.id.tv_shaixuan);
        editSearch = findViewById(R.id.edit_search);
        recycler = findViewById(R.id.recycler);
        drawer = findViewById(R.id.drawer);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new ShenheRwbAdapter(R.layout.item_shenhe_renwubao, null));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!AppUtil.isFastClick())
                    startActivity(new Intent(ShenhebaoActivity.this, ShenheRwbDetailActivity.class).putExtra("id", adapter.getData().get(position).getId()));
            }
        });
        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        adapter.isFirstOnly(false);
        adapter.openLoadAnimation();
        adapter.openLoadMore(10, true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                hideSoft(recycler);
                req.setKeyWord(editSearch.getText().toString().trim());
                refresh();
            }
        });
        adapter.setOnRecyclerViewItemLongClickListener(new BaseQuickAdapter.OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, final int position) {
                showAlertDialog("", "确定要删除吗?", new DialogUtil.DialogButtonClickListener() {
                    @Override
                    public void onPositiveButtonClick(DialogInterface dialog) {
                        presenter.deleteAudit(adapter.getData().get(position).getId());
                        dialog.dismiss();
                    }

                    @Override
                    public void onNegativeButtonClick(DialogInterface dialog) {
                        dialog.dismiss();
                    }
                });
                return true;
            }
        });
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            //发送请求
                            req.setKeyWord(v.getText().toString().trim());
                            refresh();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });

        chooseShenhePop = new ChooseShenhePop(this);
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseShenhePop.show();
            }
        });
        chooseShenhePop.setListener(new ChooseShenhePop.ClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.btn_shoudong_yinan:
                        if (!AppUtil.isFastClick())
                            startActivity(new Intent(ShenhebaoActivity.this, ExampleChooseActivity.class).putExtra("type", "2"));
                        break;
                    case R.id.btn_zidong:
                        if (!AppUtil.isFastClick())
                            startActivity(new Intent(ShenhebaoActivity.this, SendPackageActivity.class).putExtra("type", "2"));
                        break;
                }
            }
        });
        initDrawer();
        refresh();
    }

    private void initDrawer() {
        RadioGroup rgSh = findViewById(R.id.rg_sh);
        rb_sh1 = findViewById(R.id.rb_sh1);
        rb_sh2 = findViewById(R.id.rb_sh2);

        RadioGroup rgStatus = findViewById(R.id.rg_status);
        rb_s1 = findViewById(R.id.rb_s1);
        rb_s2 = findViewById(R.id.rb_s2);


        btnChongzhi = findViewById(R.id.btn_chongzhi);
        btnConfirm = findViewById(R.id.btn_confirm);

        rgSh.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_sh1:
                        req.setType("10");
                        break;
                    case R.id.rb_sh2:
                        req.setType("20");
                        break;
                }
            }
        });

        rgStatus.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_s1:
                        req.setStatus("10");
                        break;
                    case R.id.rb_s2:
                        req.setStatus("20");
                        break;
                }
            }
        });


        btnChongzhi.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        tvShaixuan.setOnClickListener(this);
    }

    private void loadMore() {
        req.setPageNo(1 + req.getPageNo());
        presenter.getList(req, refreshLayout, false);
    }

    private void refresh() {
        req.setPageNo(1);
        presenter.getList(req, refreshLayout, false);
    }

    @Override
    public void getListSuc(AuditListBean bean) {
        if (req.getPageNo() == 1) {
            refreshLayout.setRefreshing(false);
            adapter.setNewData(bean.getResult());
        } else {
            adapter.addData(bean.getResult());
        }
        adapter.openLoadMore(bean.getTotalItems() > adapter.getItemCount());
    }

    @Override
    public void deleteAuditSuc() {
        refresh();
    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        showToast(msg);
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_shaixuan:
                drawer.openDrawer(Gravity.RIGHT);
                break;
            case R.id.btn_chongzhi:
                drawer.closeDrawer(Gravity.RIGHT);
                editSearch.setText("");
                req.setKeyWord("");
                req.setStatus("");
                req.setType("");
                req.setStatus("");
                rb_sh1.setChecked(false);
                rb_sh2.setChecked(false);
                rb_s1.setChecked(false);
                rb_s2.setChecked(false);
                refresh();
                break;
            case R.id.btn_confirm:
                drawer.closeDrawer(Gravity.RIGHT);
                editSearch.setText("");
                req.setKeyWord("");
                refresh();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AuditZdEvent event) {
        refresh();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AddAuditSDEvent event) {
        refresh();
    }
}
